function queryAllCourse() {
    $.ajax({
        url: "/teach_sys/CourseServlet",
        data: {
            "action": "queryAllCourse"
        },
        success: function (value) {
            let tbody = $("#tableCourse tbody");
            let result = JSON.parse(value);
            tbody.empty();
            for (let i = 0; i < result.length; i++) {
                tbody.append("<tr>" +
                    "<td><input class='form-check-input' name='checkOne' type='checkbox' onclick='Course_getChecked()'/></td>" +
                    "<td>" + (i + 1) + "</td>" +
                    "<td>" + result[i].Cno + "</td>" +
                    "<td>" + result[i].Cname + "</td>" +
                    "<td>" + result[i].Cpno + "</td>" +
                    "<td>" + result[i].Ccredit + "</td>" +
                    "</tr>");
            }
        },
        error: function () {

        }
    });
}

function queryCourseByCno() {
    $.ajax({
        url: "/teach_sys/CourseServlet",
        data: {
            "action": "queryCourseByCno",
            "Cno": "201215121"
        },
        success: function (value) {
            console.log(value);
        },
        error: function () {

        }
    });
}

function queryCourseByCname() {
    $.ajax({
        url: "/teach_sys/CourseServlet",
        data: {
            "action": "queryCourseByCname",
            "Cname": "201215121"
        },
        success: function (value) {
            console.log(value);
        },
        error: function () {

        }
    });
}

function addCourse() {
    let form = $("#addCourseForm");
    let formData = form.serializeArray();
    let formObject = {};
    $.each(formData, function (i, item) {
        formObject[item.name] = item.value;
    });
    formObject = JSON.stringify(formObject);
    $.ajax({
        url: "/teach_sys/CourseServlet",
        data: {
            "action": "addCourse",
            "formObject": formObject    //往后端传递的是一个Course类型的JSON字符串
        },
        success: function () {
            $("#addCourse_staticBackdrop").modal('hide');
            queryAllCourse();
        },
        error: function () {
            $("#addCourse_staticBackdrop").modal('hide');
        }
    });
}

function Course_getChecked() {
    let allChecked = $("input[name='checkOne']:checked");//选中的复选框,可能不止一条
    let checkedArray = [];
    allChecked.each(function () {
        let tr = $(this).parent("td").parent("tr");//选中行的数据
        checkedArray.push(tr);//把每行的内容加入数组
    });
    return checkedArray;//返回的是勾选的所有行
}

function deleteCourse() {
    let checkedArray = Course_getChecked();
    let checkedCnoArray = [];
    for (let i = 0; i < checkedArray.length; i++) {
        let td = checkedArray[i].find("td");//选中第i行每一列的数据
        checkedCnoArray.push(td[2].innerText);//把第3列的内容加入数组，格式为1,2,3,11,11以逗号间隔
    }
    checkedCnoArray = JSON.stringify(checkedCnoArray);
    $.ajax({
        url: "/teach_sys/CourseServlet",
        data: {
            "action": "deleteCourse",
            "Cno": checkedCnoArray,
        },
        success: function () {
            queryAllCourse();
        },
        error: function () {

        }
    });
}

function updateCourseModal() {
    let checkedArray = Course_getChecked();
    if (checkedArray.length !== 0) {
        $("#updateCourse_staticBackdrop").modal('show');
        //选中每一列的数据并赋值
        let td = checkedArray[0].find("td");
        $("#updateCno").val(td[2].innerText);
        $("#updateCname").val(td[3].innerText);
        $("#updateCpno").val(td[4].innerText);
        $("#updateCcredit").val(td[5].innerText);
    }
}

function updateCourse() {
    let form = $("#updateCourseForm");
    let formData = form.serializeArray();
    let formObject = {};
    $.each(formData, function (i, item) {
        formObject[item.name] = item.value;
    });
    formObject = JSON.stringify(formObject);
    $.ajax({
        url: "/teach_sys/CourseServlet",
        data: {
            "action": "updateCourse",
            "formObject": formObject    //往后端传递的是一个Course类型的JSON字符串
        },
        success: function () {
            $("#updateCourse_staticBackdrop").modal('hide');
            queryAllCourse();
        },
        error: function () {
            $("#updateCourse_staticBackdrop").modal('hide');
        }
    });
}